/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cocos2d.game.scene;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import tbgb.BaseGame;
import tbgb.process.render.RenderProcess;
import tbgb.process.update.UpdateProcess;

/**
 *
 * @author KieuAnh
 */
public class CCSceneManager implements UpdateProcess, RenderProcess
{
    private final Vector screens = new Vector();
    private boolean saveScreen = false;
    private final CCSceneBank bank;
    
    public CCSceneManager(BaseGame game, CCSceneBank bank)
    {
        this.bank = bank;
    }
    
    public void update(long eslapedGameTime)
    {
        for (int i = this.screens.size() - 1; i >= 0; --i)
        {
            Object sceneObj = this.screens.elementAt(i);
            if (sceneObj instanceof CCScene)
            {
                CCScene scene = (CCScene) sceneObj;
                if (scene.isEnabled())
                {
                    scene.update(eslapedGameTime);
                }
            }
        }
    }
    
    public void render(Graphics g, long eslapedGameTime)
    {
        for (int i = 0; i < this.screens.size(); ++i)
        {
            Object sceneObj = this.screens.elementAt(i);
            if (sceneObj instanceof CCScene)
            {
                CCScene scene = (CCScene) sceneObj;
                if (scene.isVisibled())
                {
                    scene.render(g, eslapedGameTime);
                }
            }
        }
    }
    
    public CCScene Current()
    {
        if (this.screens.size() > 0)
        {
            return (CCScene) this.screens.elementAt(this.screens.size() - 1);
        }

        return null;
    }
    
    
    public void AddExclusive(CCScene screen)
    {
        if (this.Current() != null)
        {
            this.Current().setState(CCScene.Deactivated);
        }

        screen.setState(CCScene.Activated);
        this.AddScreen(screen);
    }

    public void AddPopup(CCScene screen)
    {
        if (this.Current() != null)
        {
            this.Current().setState(CCScene.Paused);
        }

        screen.setState(CCScene.Activated);
        this.AddScreen(screen);
    }

    public void AddImmediately(CCScene screen)
    {
        this.AddScreen(screen);
    }

    public void RemoveCurrent()
    {
        this.Current().setState(CCScene.Deactivated);
        this.screens.removeElementAt(this.screens.size() - 1);

        if (this.screens.size() > 0)
        {
            this.Current().setState(CCScene.Activated);
        }
    }

    public void RemoveScreen(CCScene screen)
    {
        for (int i = 0; i < this.screens.size(); )
        {
            Object sceneObj = this.screens.elementAt(i);
            if (sceneObj instanceof CCScene)
            {
                CCScene scene = (CCScene) sceneObj;
                if (scene.getName().equals(screen.getName()))
                {
                    this.screens.removeElementAt(i);
                }
                else
                {
                    ++i;
                }
            }
        }
    }

    private void AddScreen(CCScene screen)
    {
        if (!this.isSaveScreen())
        {
            this.RemoveScreen(screen);
        }

        this.screens.addElement(screen);
    }

    /**
     * @return the saveScreen
     */
    public boolean isSaveScreen() {
        return saveScreen;
    }

    /**
     * @param SaveScreen the saveScreen to set
     */
    public void setSaveScreen(boolean SaveScreen) {
        this.saveScreen = SaveScreen;
    }
}
